/**
 * @copyright
 * ====================================================================
 * Copyright (c) 2006 Xiaoniu.org.  All rights reserved.
 *
 * This software is licensed as described in the file LICENSE, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://code.google.com/p/suafe/.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://code.google.com/p/suafe/.
 * ====================================================================
 * @endcopyright
 */
package org.xiaoniu.suafe.resources;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import org.xiaoniu.suafe.Constants;

/**
 * Utility class used to access application resources.
 * 
 * @author Shaun Johnson
 */
public final class ResourceUtil {

	/**
	 * Current resource bundle.
	 */
	protected static ResourceBundle bundle;

	/**
	 * Gets the current resource bundle.
	 * 
	 * @return
	 */
	protected static ResourceBundle getBundle() {
		if (bundle == null) {
			bundle = ResourceBundle.getBundle(Constants.PATH_RESOURCE_BUNDLE);
		}

		return bundle;
	}

	/**
	 * Retrieves a formatted string from the resource bundle.
	 * 
	 * @param name Name of string.
	 * @param arg1 Int argument
	 * @param arg2 String argument
	 * @return Formatted string.
	 */
	public static String getFormattedString(final String name, final int arg1, final String arg2) {
		final Object[] args = new Object[2];

		args[0] = arg1;
		args[1] = arg2;

		return MessageFormat.format(getBundle().getString(name), args);
	}

	/**
	 * Retrieves a formatted string from the resource bundle.
	 * 
	 * @param name Name of string.
	 * @param args String arguments
	 * @return Formatted string.
	 */
	public static String getFormattedString(final String name, final Object[] args) {
		return MessageFormat.format(getBundle().getString(name), args);
	}

	/**
	 * Retrieves a formatted string from the resource bundle.
	 * 
	 * @param name Name of string.
	 * @param arg String argument
	 * @return Formatted string.
	 */
	public static String getFormattedString(final String name, final String arg) {
		final Object[] args = new Object[1];

		args[0] = arg;

		return MessageFormat.format(getBundle().getString(name), args);
	}

	/**
	 * Retrieves a string from the resource bundle.
	 * 
	 * @param name Name of string.
	 * @return String from resource bundle.
	 */
	public static String getString(final String name) {
		return getBundle().getString(name);
	}
}
